using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace CreatingRainbows
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MainRoom : GameScene
    {
        Door magentaDoor;
        Texture2D magentaDoorImg;

        Door supriseDoor;
        Texture2D supriseDoorImg;

        SpriteBatch spriteBatch;

        int enterance;
        public int Enterance
        {
            get { return enterance; }
            set { enterance = value; }
        }

        bool lvl1UltGoal;
        public bool Lvl1UltGoal
        {
            get { return lvl1UltGoal; }
            set { lvl1UltGoal = value; }
        }

        bool lvl1Restored;

        public MainRoom(Game1 game, Texture2D textureBack, Texture2D textureFront)
            : base(game)
        {
            mainReference = game;

            Components.Add(new ImageComponent(game, textureBack, ImageComponent.DrawMode.Stretch));
            Components.Add(new ImageComponent(game, textureFront, ImageComponent.DrawMode.Center));

            Initialize();
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            lvl1Restored = false;
            lvl1UltGoal = false;
            
            magentaDoor = new Door(mainReference);
            this.Components.Add(magentaDoor);
            magentaDoor.Initialize(Vector2.Zero, new Rectangle(0, 0, 25, 100), Color.White,
                             Vector2.Zero, new Vector2(1, 1), 0.0f);

            supriseDoor = new Door(mainReference);
            supriseDoor.Initialize(Vector2.Zero, new Rectangle(0, 0, 209, 119), Color.White,
                             Vector2.Zero, new Vector2(1, 1), 0.0f);

            base.Initialize();

            LoadContent();

            
        }

        protected override void LoadContent()
        {
            magentaDoorImg = mainReference.Content.Load<Texture2D>("magentaDoor");
            magentaDoor.LoadContent(spriteBatch, ref magentaDoorImg);

            supriseDoorImg = mainReference.Content.Load<Texture2D>("suprise");
            supriseDoor.LoadContent(spriteBatch, ref supriseDoorImg);

            enteranceLocs = new List<Vector2>();
            enteranceLocs.Add(new Vector2(magentaDoor.position.X + magentaDoor.source.Width + 5, (magentaDoor.source.Height / 2) - main.source.Height / 2));

            
            base.LoadContent();

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            DoorCollision();

            base.Update(gameTime);
        }

        private void DoorCollision()
        {
            if (main.Colliding(magentaDoor))
            {
                this.Hide();
                mainReference.MagentaRoomScene.Enterance = 1;
                mainReference.MagentaRoomScene.Show();

                mainReference.ActiveScene = mainReference.MagentaRoomScene;
            }
        }

        public override void Show()
        {
            //main.position = Localize((mainReference.SceneWidth / 2) - (main.source.Width / 2), (mainReference.SceneHeight / 2) - (main.source.Width / 2));
            //main.position.X = mainReference.ScreenWidth / 2 - main.source.Width / 2;
            //main.position.Y = mainReference.ScreenHeight / 2 - main.source.Height / 2;

            magentaDoor.position = Localize(0, 0);
            supriseDoor.position = Localize(100, 100);
            //magentaDoor.position.Y = (mainReference.ScreenHeight / 2) - (mainReference.SceneHeight / 2);
            //magentaDoor.position.X = (mainReference.ScreenWidth / 2) - (mainReference.SceneWidth / 2);

            Enter(enterance);

            CheckGoals();

            base.Show();
        }

        public override void CheckGoals()
        {
            if (!lvl1Restored)
            {
                if (lvl1UltGoal)
                {
                    this.Components.Add(supriseDoor);
                    lvl1UltGoal = true;
                }
            }
            base.CheckGoals();
        }

        public void Enter(int enterance1)
        {
            switch (enterance1)
            {
                case 1:
                    main.position = Localize(enteranceLocs[0].X, enteranceLocs[0].Y);
                    break;
                default:
                    main.position = Localize((mainReference.SceneWidth / 2) - (main.source.Width / 2), (mainReference.SceneHeight / 2) - (main.source.Width / 2));
                    break;
            }
        }
    }
}
